package connections;

import main.Client;
import message.FileWasChanged;
import main.UserInfo;
import message.Info;
import message.Message;
import message.NewBox;
import message.SendFile;
import message.StartComunication;
import message.StartSync;
import message.StopComunication;
import message.TransferFile;

/**
 * handle the message from the server 
 * @author ran,barak
 *
 */
public class MessageHandler {
	private Client client = Client.getClientInstance();
	private UserInfo userInfo = UserInfo.getInstance();

	/**
	 * interpret the message and return the replay message
	 * @param message the message from the server
	 * @return return a replay message
	 */
	public Message interpretMessage(Message message){
		System.out.println("handling message type: "+ message);
		Message responseMessage=null;
		// check the type of the message
		switch(message.getType()){
		case START_COMUNICATION:
			responseMessage = startComunicationMC();
			break;
		case FILE_WAS_CHANGED:
			responseMessage = fileWasChangedMC(message);
			break;
		case TRANSFER_FILE:
			responseMessage = transferFileMC(message);
			break;
		case SEND_FILE:
			responseMessage = sendFileMC(message);
			break;
		case STOP_COMUNICATION:
			responseMessage = stopComunicationMC();
			break;
		case INFO:
			responseMessage = infoMC(message);
			break;
		case STOP_SYNC:
			responseMessage=new StopComunication();
			break;
		case NEW_BOX:
			responseMessage= newBoxMC(message);
			break;
		}
		System.out.println(message.getType());
		return responseMessage;
	}
	/*
	 * handle the NewBox message
	 * @param message the message 
	 * @return message to replay
	 */
	private Message newBoxMC(Message message) {
		NewBox newBox=(NewBox)message;
		//create new folder
		client.createNewFolder(newBox.getBoxName());
		// register to the server
		client.registerToServer();
		return new StopComunication();
	}
	/*
	 * handle the Info message
	 * @param message the message 
	 * @return message to replay
	 */
	private Message infoMC(Message message) {
		Info infoMessage=(Info)message;
		// get user info
		UserInfo userInfo=UserInfo.getInstance();
		// update user info
		userInfo.setUserName(infoMessage.getUserName());
		userInfo.setPass(infoMessage.getPass());
		System.out.println("size of boxes list : "+infoMessage.getBoxesList().size());
		userInfo.setBoxesList(infoMessage.getBoxesList());
		System.out.println("infoMC get user Info : "+userInfo.getBoxesList().size());
		userInfo.setEmail(infoMessage.getEmail());
		userInfo.setUpdate(true);
		System.out.println("register to server");
		return new StartSync();
	}
	
	/*
	 * handle the stopComunication message
	 * @param message the message 
	 * @return message to replay
	 */
	private Message stopComunicationMC() {
		System.out.println("stop comunication");
		// check if there is a message to send to the server
		if(client.isMsgWait()){
			System.out.println("message wait");
			// return the next message
			return client.getNextMessage();

		}
		else{
			// register to the server
			System.out.println("register to server");
			client.registerToServer();
		}
		Message m = new StopComunication();
		return m;
	}
	/*
	 * handle the startComunication message
	 * @param message the message 
	 * @return message to replay
	 */
	private Message startComunicationMC() {
		return new StartComunication("server", "server","server");
	}
	/*
	 * handle the transferFile message
	 * @param message the message 
	 * @return message to replay
	 */
	private Message transferFileMC(Message message) {
		TransferFile transferFile = (TransferFile)message;
		// create the file on the client computer
		client.createFile(transferFile.getBoxName(),transferFile.getFileName(), transferFile.getByteArray());
		return new StopComunication();
	}
	/*
	 * handle the fileWasChanged message
	 * @param message the message 
	 * @return message to replay
	 */
	private Message fileWasChangedMC(Message message){
		FileWasChanged fileWasChanged = (FileWasChanged)message;
		return client.fileWasChanged(fileWasChanged);
	}
	/*
	 * handle the sendFile message
	 * @param message the message 
	 * @return message to replay
	 */
	private Message sendFileMC(Message message){
		SendFile sendFile = (SendFile)message;
		System.out.println("file Path : "+sendFile.getFilePath());
		// get the file bytes to send 
		byte[] byteArray = client.getFileBytes(userInfo.getPath()+"\\"+sendFile.getFilePath());
		System.out.println("client sendFileMC : "+sendFile.getFileName());
		int i=sendFile.getFilePath().lastIndexOf("\\");
		// get the file name
		String path=sendFile.getFilePath().substring(0, i);
		return new TransferFile(sendFile.getFileName(),byteArray,path,sendFile.getCheckSum(),sendFile.getFileWasChanged());
	}
}
